<?php


namespace app\admin\controller;


use app\common\controller\Common;
use app\index\controller\Base;
use think\Db;
use think\Exception;
use think\Session;

class City extends Base
{

    //城市列表
    public function index()
    {
        //权限查询
        $common = new Common();
        $powerList = $common->showpower();
        $this->assign("powerList",$powerList);

        $keyword = '';
        $time = '';
        $area = '';
        $phone = '';

        //根据平台区分
        $thisWechat = Session::get("thisWechat");
        $where = "b.uniacid =".$thisWechat['id'];

        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where .=" and b.name like '%".$keyword."%'";
        }
        if(!empty($_GET['phone'])){
            $phone = $_GET['phone'];
            $where .=" and b.phone like '%".$phone."%'";
        }
        if(!empty($_GET['time'])){
            $time = $_GET['time'];
            $serach = explode(' - ',$time);
            $starttime = strtotime($serach[0]);
            $endttime = strtotime($serach[1]." 23:59:59");

            $where .=" and ".$starttime." <=a.addtime and a.addtime<=".$endttime;
        }
        if(!empty($_GET['area'])){
            //获取区域信息
            $where1['name'] = ['like',"%".$_GET['area']."%"];
            $where1['level'] = ['elt',"3"];
            $region = Db::name("admin_region")->field("id,level,name,code")->where($where1)->select();
            $region_val = array_column($region,"id");
            $region_val = implode(",",$region_val);
            $area = $_GET['area'];
            $where .=" and (a.province in ('".$region_val."') or a.city in ('".$region_val."') or a.area in('".$region_val."'))";
        }


        if(!empty($_GET['type'])){
            $page = empty($_GET['page'])?1:$_GET['page'];
            $limit = empty($_GET['limit'])?10:$_GET['limit'];
            $start = ($page-1)*$limit;

            //链表查询
            $list = Db::name("admin_agent_city")->alias("a")->join("admin_agent b", "a.agent_id = b.id")
                ->field("a.id as a_id,a.province,a.city,a.area,a.addtime as a_addtime,a.status as a_status,b.*")
                ->where($where)->limit($start,$limit)->select();


            //统计终端数量
            foreach ($list as $k => $v) {
                $list[$k]['count'] = 0;
                //查询代理城市
                $list[$k]['province_name'] = Db::name("admin_region")->where(['id' => $v['area']])->value('name');
                $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['a_addtime']);
            }


            //链表查询
            $count = Db::name("admin_agent_city")->alias("a")->join("admin_agent b", "a.agent_id = b.id")->where($where)->limit($start,$limit)->count("*");;

            $black['code'] = 0;
            $black['data'] = $list;
            $black['count'] = $count;
            return $black;

        }




        $assign = [
            "keyword"=>$keyword,
            "time"=>$time,
            "area"=>$area,
            "phone"=>$phone
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }


    //更新状态
    public function clickStatus()
    {
        try {
            $common = new Common();
            $data['status'] = $_POST['status'];
            Db::name("admin_agent_city")->where(['id' => $_POST['id']])->update($data);
            $log['operator'] = Session::get("user_name");
            $log['operator_id'] = Session::get("id");
            $log['addtime'] = time();
            $log['ip'] = request()->ip();
            $log['controller'] = "City";
            $log['methods'] = "clickStatus";
            $area = Db::name("admin_agent_city")->where(['id' => $_POST['id']])->value("area");
            $name = Db::name("admin_region")->where(['id' => $area])->value("name");
            if ($_POST['status'] == 1) {
                $log['remark'] = "管理员【" . Session::get("user_name") . "】开启代理区域" . $name;
            } else {
                $log['remark'] = "管理员【" . Session::get("user_name") . "】关闭代理区域" . $name;
            }
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //删除区域
    public function delCity(){
        try{
            $id = $_POST['id'];
            $city = Db::name("admin_agent_city")->where(['id'=>['in',$id]])->field("area")->select();
            $res = Db::name("admin_agent_city")->where(['id'=>['in',$id]])->delete();
            if(empty($res)){
                throw new Exception("删除失败");
            }
            $name = "";
            foreach ($city as $k=>$v){
                $region_name = Db::name("admin_region")->where(['id'=>$v['area']])->value("name");
                $name .= $region_name.",";
            }
            $common = new Common();
            $log['remark'] = "管理员【" . Session::get("user_name") . "】删除代理区域【".$name."】";
            $log['controller'] = "City";
            $log['methods'] = "delCity";
            $common->addLog($log);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


}